<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/24 0024
  Time: 14:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>工序物料设计单子页面</title>
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/manufactureCss/b/Process_material_design _sheet.css"/>
    <style type="text/css">

    </style>
</head>
<body style="background-color: #eeeeee;">
<div class="panel-heading hhead">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <span style="color: blue;">您正在做的业务是：</span>
            <li class="breadcrumb-item"><a href="#">生产管理</a></li>
            <li class="breadcrumb-item"><a href="#">工序物料设计</a></li>
            <li class="breadcrumb-item active" aria-current="page">制定工序物料设计单</li>
        </ol>
    </nav>
</div>
<div style="margin-bottom: 2px">
    <div style="display: inline;margin-left: 86%;">
        <button class="btn btn-primary">预览</button>
        <button class="btn btn-primary">返回</button>
    </div>
</div> <!-- 此div改用栅格来做 -->
<div id="designListBox" style="margin:10px ">
    <div style="text-align: center">
        <h3>工序物料设计单</h3>
    </div>
    <div style="padding: 30px">
        <table id="tbl2" class="table table-bordered" style="margin: 15px auto;">
            <thead>
            <tr>
                <th>序号</th><th width="200px">物料名称</th><th>物料编号</th>
                <th width="200px">描述</th><th>设计数量</th><th>可用数量</th>
                <th>单位</th><th>单价（元）</th><th>本工序数量</th>
            </tr>
            </thead>
            <tbody id="template">
            <tr>
                <td>1</td>
                <td>aaaaaa</td>
                <td>aaaa</td>
                <td>a</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td style="background: #fdffdc;"><input style="height: 25px" class="num-input"></td></td>
            </tr>
            </tbody>

            <span>制造：<label></label>产品，供选择的物料清单如下：</span>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript">
    $(function (){
        initialMaterialTable();

        $("#template").on("blur",".num-input",function () {
            let value = $(this).val();
            let reg = /^[0-9]*$/;
            if (!reg.test(value)) {
                alert("请输入数字");
                $(this).val("");
                return;
            }else if (value !="") {
                let amount = $(this).parent().parent().find("td:eq(4)").text();
                let availableAmount = $(this).parent().parent().find("td:eq(5)").text();
                let id = $(this).parent().parent().find("input:eq(0)").attr("id");
                console.log(value > availableAmount)
                if (parseInt(value) > parseInt(availableAmount)) {
                    alert("库存不足");
                    $(this).val("");
                    $(this).parent().parent().find("td:eq(5)").text(sessionStorage.getItem(id));
                    return;
                }
                if (parseInt(value) > parseInt(amount)) {
                    alert("数量不能大于设计数量");
                    $(this).val("");
                    $(this).parent().parent().find("td:eq(5)").text(sessionStorage.getItem(id));
                    return;
                }
                let availableAmount2 = parseInt(availableAmount) - parseInt(value);
                sessionStorage.setItem(id,availableAmount2);
                let procedureId = sessionStorage.getItem("procedureId")
                let costPrice = $(this).parent().parent().find("td:eq(7)").text();
                let data = sessionStorage.getItem(procedureId)!=null?JSON.parse(sessionStorage.getItem(procedureId)):{};
                if (data == null) {
                    data = {}
                }
                let data2 = data[procedureId];
                console.log(data2)
                if (data2 == null) {
                    data2 = []
                }
                let flag = false;
                for (let i = 0; i < data2.length; i++) {
                    if (data2[i].productId == id) {
                        data2[i].amount = value;
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    data2.push(
                        {
                            "productId":id,
                            "amount":value,
                            "costPrice":costPrice
                        }
                    )
                }
                data[procedureId] = data2
                sessionStorage.setItem(procedureId,JSON.stringify(data));
                $(this).parent().parent().find("td:eq(5)").text(availableAmount2);
            } else {
                let id = $(this).parent().parent().find("input:eq(0)").attr("id");
                let availableAmount = sessionStorage.getItem(id);
                $(this).parent().parent().find("td:eq(5)").text(availableAmount);   // 恢复库存
            }
        })
    })
    function initialMaterialTable() {
        let procedureId = sessionStorage.getItem("procedureId");
        let productId = sessionStorage.getItem("productId")
        $.ajax({
            url:"/d-module-details/initialProcessMaterial?productId="+productId,
            type:"get",
            success:function (data) {
                console.log(data);
                let template = $("#template");
                let html = "";
                template.html("");
                for (let i = 0; i < data.length; i++) {
                    let productId = sessionStorage.getItem(data[i].productId);
                    if (productId == null) {
                        sessionStorage.setItem(data[i].productId,data[i].amount);
                    }
                    html += "<tr>" +"<input type='hidden' id='"+data[i].productId+"'>"+
                        "<td>"+(i+1)+"</td>" +
                        "<td>"+data[i].productName+"</td>" +
                        "<td>"+data[i].productId+"</td>" +
                        "<td>"+data[i].productDescribe+"</td>" +
                        "<td>"+data[i].amount+"</td>" +
                        "<td>"+sessionStorage.getItem(data[i].productId)+"</td>" +
                        "<td>"+data[i].amountUnit+"</td>" +
                        "<td>"+data[i].costPrice+"</td>" +
                        "<td style='background-color: #fdffdc;'><input style='height: 25px' class='num-input'></td>" +
                        "</tr>";
                }
                template.html(html);

            }
        })
    }
</script>
</body>
</html>

